﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class Pager : System.Web.UI.UserControl
{
    PagedDataSource _pageDataSource;
    Repeater repeater;
    public event System.EventHandler ChangePage;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            _pageDataSource = new PagedDataSource();
            _pageDataSource.AllowPaging = true;

        }
    }


    public Repeater BindToControl
    {
        set { repeater = value;
        repeater.DataBinding += new EventHandler(repeater_DataBinding);
        }
    }

    void repeater_DataBinding(object sender, EventArgs e)
    {
        LoadPageLink();
    }


    /// <summary>
    /// View from page
    /// </summary>
    protected int StartPage
    {
        get
        {
            return (CurrentPageIndex / NumPageView) * NumPageView;
        }
    }

    /// <summary>
    /// View to page
    /// </summary>
    protected int EndPage
    {
        get { return StartPage + NumPageView < TotalPage ? StartPage + NumPageView : TotalPage; }
    }

    public void LoadPageLink()
    {
        DataTable tbl = new DataTable();
        tbl.Columns.Add("Index");
        tbl.Columns.Add("css");
            
        btnFirst.Visible = StartPage > 0;
        btnPrev.Visible = (CurrentPageIndex > 0 && ((CurrentPageIndex / NumPageView) > 0));
        btnNext.Visible = (EndPage < TotalPage);
        btnLast.Visible = EndPage < TotalPage;

        if (TotalPage <= 1)
            return;
        for (int i = StartPage; i < EndPage; i++)
        {
            if (i == CurrentPageIndex)
                tbl.Rows.Add(new object[] { i, "Index-page-item" });
            else
                tbl.Rows.Add(new object[] { i, "page-item" });
        }
        repLink.DataSource = tbl;
        repLink.DataBind();
        Label1.Text = "Trang " + (CurrentPageIndex + 1) + "/" + TotalPage;

        IsPostBackPage = false;
    }

    /// <summary>
    /// DataView of Repeater
    /// </summary>
    public DataView DataSource
    {
        set
        {
            PagedDataSource.DataSource = value;
            TotalRecord = value.Table.Rows.Count;
        }
    }

    /// <summary>
    /// PagedDataSource of repeater
    /// </summary>
    public PagedDataSource PagedDataSource
    {
        get
        {
            if (_pageDataSource == null)
            {
                _pageDataSource = new PagedDataSource();
                _pageDataSource.AllowPaging = true;

            }
            _pageDataSource.CurrentPageIndex = CurrentPageIndex;
            return _pageDataSource;
        }

    }
    /// <summary>
    /// Number of page view on screen
    /// </summary>
    public int NumPageView
    {
        get
        {
            if (this.ViewState["NumPageView"] == null)
                NumPageView = 3;
            return (int)this.ViewState["NumPageView"];
        }
        set
        {
            this.ViewState["NumPageView"] = value;
        }
    }
    /// <summary>
    /// Number record in one page
    /// </summary>
    public int PageSize
    {
        get
        {
            if (this.ViewState["PageSize"] == null)
                PageSize = 2;
            return (int)this.ViewState["PageSize"];
        }
        set
        {
            this.ViewState["PageSize"] = value;
            PagedDataSource.PageSize = PageSize;
        }
    }
    /// <summary>
    /// CSS of selected page
    /// </summary>
    public string CurrentPageCSS
    {
        get
        {
            if (this.ViewState["CurrentPageCSS"] == null)
                CurrentPageCSS = "Pager_CurrentPage";
            return (string)this.ViewState["CurrentPageCSS"];
        }
        set { this.ViewState["CurrentPageCSS"] = value; }
    }
    /// <summary>
    /// CSS of page navigation link
    /// </summary>
    public string PageIndexCSS
    {
        get
        {
            if (this.ViewState["PageIndexCSS"] == null)
                PageIndexCSS = "page";
            return (string)this.ViewState["PageIndexCSS"];
        }
        set { this.ViewState["PageIndexCSS"] = value; }
    }

    /// <summary>
    /// Total record of datasource
    /// </summary>
    public int TotalRecord
    {
        get
        {
            if (this.ViewState["TotalRecord"] == null)
                TotalRecord = 0;
            return (int)this.ViewState["TotalRecord"];
        }
        set { this.ViewState["TotalRecord"] = value; }
    }
    /// <summary>
    /// Total page of datasource
    /// </summary>
    public int TotalPage
    {
        get
        {
            return  (int) Math.Ceiling( 1.0*TotalRecord / PageSize);
        }
    }
    /// <summary>
    /// Current Page
    /// </summary>
    public int CurrentPageIndex
    {
        get
        {
            if (IsPostBackPage == false)
                return 0;
            if (this.ViewState["Pager_CurrentPage"] == null)
                CurrentPageIndex = 0;
            return (int)this.ViewState["Pager_CurrentPage"];
        }
        set { this.ViewState["Pager_CurrentPage"] = value; }
    }

    protected void CallChangeEvent(EventArgs e)
    {
        ChangePage(this, e);
        IsPostBackPage = true;
    }

    protected void repLink_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        CurrentPageIndex = int.Parse(e.CommandArgument.ToString());
        //
        CallChangeEvent(e);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        if (CurrentPageIndex < TotalPage)
        {
            CurrentPageIndex = CurrentPageIndex + 1;
            CallChangeEvent(e);
        }
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        if (CurrentPageIndex > 0)
        {
            CurrentPageIndex = CurrentPageIndex - 1;
            CallChangeEvent(e);
        }
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPageIndex = 0;
        CallChangeEvent(e);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        CurrentPageIndex = TotalPage - 1;
        CallChangeEvent(e);
    }

    protected bool IsPostBackPage
    {
        get
        {
            return true;
            //if (this.ViewState["IsPostBackPage"] == null)
            //    IsPostBackPage = false;
            //return (bool)this.ViewState["IsPostBackPage"];
        }

        set { this.ViewState["IsPostBackPage"] = value; }
    }
}
